package com.wzy.java;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class test25 {
    public static List<User> createVOList() {
        List<User> list = new ArrayList<>();


        User user1 = new User();
        user1.setId(1);
        user1.setName("wzy1");
        user1.setAge(23);
        user1.setMonth(1);
        list.add(user1);

        User user2 = new User();
        user2.setId(2);
        user2.setName("wzy2");
        user2.setAge(23);
        user2.setMonth(2);
        list.add(user2);

        User user3 = new User();
        user3.setId(3);
        user3.setName("wzy3");
        user3.setAge(23);
        user3.setMonth(3);
        list.add(user3);

        User user4 = new User();
        user4.setId(4);
        user4.setName("wzy4");
        user4.setAge(23);
        user4.setMonth(4);
        list.add(user4);

        User user5 = new User();
        user5.setId(5);
        user5.setName("wzy5");
        user5.setAge(23);
        user5.setMonth(5);
        list.add(user5);

        User user6 = new User();
        user6.setId(6);
        user6.setName("wzy6");
        user6.setAge(23);
        user6.setMonth(6);
        list.add(user6);
        return list;
    }

    public static void main(String[] args) {


// 获取对象属性
        Field[] declaredFields = User.class.getDeclaredFields();
        User user1 = null;
        try {
            user1 = User.class.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<User> voList = createVOList();
        for (User user : voList) {
            for (Field field : declaredFields) {
                String name = field.getName();
                field.setAccessible(true);
                try {
                    Object o = field.get(user1);
                    System.out.println("输出获取的值：" + o);

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

/*        for (Field field : declaredFields) {
            String name = field.getName();
            field.setAccessible(true); // 私有属性必须设置访问权限
            Object resultValue = field.get(obj);
            // 这里可以编写你的业务代码
            System.out.println(name + ": " + resultValue);
        }*/

        /*List<User> voList = createVOList();

        List<Users> resultList = new ArrayList<>();
        Field[] fields = new Field[12];
        for (int i = 0; i < 6; i++) {
            try {
                Field declaredField = Users.class.getDeclaredField("month" + (i + 1));
                fields[i] = declaredField;
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        Integer month = null;
        for (User user : voList) {
            Users users = new Users();
            users.setId(user.getId());
            users.setName(user.getName());
            month = user.getMonth();
            for (int i = 0; i < fields.length; i++) {
                try {
                    fields[i-1].setAccessible(true);
                    if (fields[i].getName().equals("month" + (i + 1))) {
                        fields[i].set(users, month);
                    }
                    resultList.add(users);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        for (Users users : resultList) {
            System.out.println(users);
        }*/
    }
}
